package com.example.demo.web;

import com.baidu.ueditor.ActionEnter;
import com.google.common.io.Resources;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by lei on 2017/5/20.
 */
@Controller
@RequestMapping("/ueditor")
public class UEditorController {

	private static final String uploadPath = "/Users/lei/Documents/java_workspace/ueditor-demo/src/main/resources";

	@RequestMapping(value = "",method = RequestMethod.GET)
	public String index(){
		return "index";
	}

	@RequestMapping(value = "/upload")
	public void upload(HttpServletRequest request, HttpServletResponse response) throws IOException {
		request.setCharacterEncoding( "utf-8" );
		response.setHeader("Content-Type" , "text/html");

		String rootPath = request.getServletContext().getRealPath("/");
		ServletOutputStream out = response.getOutputStream();
		/**
		 * 获取ueditor的config文件
		 */
		String configJsonFilePath = Resources.getResource("static/ueditor/config.json").getFile();
		String retString = new ActionEnter( request, rootPath,uploadPath, configJsonFilePath).exec();
		out.write(retString.getBytes());
	}
}
